package com.cs474.cupcakes7;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AddIngredient extends Activity {
	
	private ArrayAdapter<CharSequence> adapterForSpinnerType;
	private ArrayAdapter<CharSequence> adapterForSpinnerUnit;
	
	private Spinner typeSpinner;
	private Spinner unitSpinner;
	
	private Button addIngredient;
	
	private EditText ingredientName;
	private EditText ingredientAmount;
	
	private Toast errorMessage;
	private Toast successMessage;
	public void onCreate(Bundle savedInstanceState)
	    {
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        
	        //Set the layout for the ingredient
	        setContentView(R.layout.add_ingredient);
	        //Initialize the messages
	        errorMessage = Toast.makeText(this, "One of the fields is empty! Try again!", 
    				Toast.LENGTH_LONG);
	        
	        ingredientName = (EditText) findViewById(R.id.ingredientTextName);
	        ingredientAmount = (EditText) findViewById(R.id.ingredientTextAmount);
	        //Find the spinner for the type and then 
	        typeSpinner = (Spinner) findViewById(R.id.ingredientSpinnerType);
	        unitSpinner = (Spinner) findViewById(R.id.ingredientSpinnerUnit);
	        //Create an adapter for the spinner
	        adapterForSpinnerType = 
	        	new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
	        adapterForSpinnerUnit = 
	        	new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
	        
	        //set the adapter for for the spinner
	        typeSpinner.setAdapter(adapterForSpinnerType);
	        unitSpinner.setAdapter(adapterForSpinnerUnit);
	        
	        //Button for add ingredient
	        addIngredient = (Button) findViewById(R.id.ingredientButtonAdd);
	        
	        //Add the basic types of ingredients
	    	adapterForSpinnerType.add("Whole Item");//index 0
	    	adapterForSpinnerType.add("Liquid");	//index 1
	    	adapterForSpinnerType.add("Powdered");	//index 2
	        
	    	adapterForSpinnerUnit.add("Small");
	    	adapterForSpinnerUnit.add("Large");
	    	
	        //Add a listener for the button...     
	        addIngredient.setOnClickListener(new OnClickListener() {
	        	@Override
	        	public void onClick(View v) {
	        		String strIngredientName = ingredientName.getText().toString();
	        		String strType 			 = adapterForSpinnerType.getItem(typeSpinner.getSelectedItemPosition()).toString();
	        		
	    	        if (strType != null && strIngredientName != null) {
		        		
		        		
		        		// Name, type, amount , unit, 
		        		addIngredient(strIngredientName, 
		        				ingredientAmount.getText().toString(),
		        				adapterForSpinnerUnit.getItem(unitSpinner.getSelectedItemPosition()).toString(),
		        				adapterForSpinnerType.getItem(typeSpinner.getSelectedItemPosition()).toString());
	    	        
	    	        }
	    	        else {
	    	        	errorMessage.show();
	    	        }	
	        	}
	        });      
	 }
	
	
	/*
	 *  Here is the function that interacts with the underlying
	 *  C++ code to add an ingredient to the list of ingredients.
	 */
	private void addIngredient(String name, String am, String un, String ty) {
		//We need to perform some conversions first...
		double amount = 0;
		int unit = 0;
		int type = 0;
		try {
			amount 	= Double.parseDouble(am);

			if (ty.equals("Liquid")) type = 1;
			else if (ty.equals("Powedered")) type = 2;
			if (un.equals("Large")) unit = 1;
			
		}
		catch (NumberFormatException e) {
			System.out.println("Parse error");
		}
		successMessage = Toast.makeText(getApplicationContext(), 
        		"Added Ingredient:\nName: " + name + " added!", Toast.LENGTH_LONG);
		successMessage.show();
		
		//Call native function
		String ret = newIngredient(name, amount, unit, type);
		//Go back 
		startActivity(new Intent(this, IngredientsList.class));
    }
	
    public native String newIngredient(String string, double am, int un, int i);

    //Load the c/c++ shared library we created 
    static {
        System.loadLibrary("cupcake");
    }
}	
